#!/bin/bash

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# Authors: Nicolas Zingilé <nicolas.zingile@open.eurogiciel.org>

tmpdir=/tmp/gerrit
outdir=$tmpdir/results
iviurl=http://download.tizen.org/snapshots/tizen/ivi/ivi
commonurl=http://download.tizen.org/snapshots/tizen/common/common-wayland-x86_64

## tmp files
commontmp1=$tmpdir/common1.tmp
commontmp2=$tmpdir/common2.tmp
ivitmp1=$tmpdir/ivi1.tmp
ivitmp2=$tmpdir/ivi2.tmp
tizentmp1=$tmpdir/tizen1.tmp
tizentmp2=$tmpdir/tizen2.tmp

## result files
commonlist=$tmpdir/common.list
commonres=$tmpdir/common.result
ivilist=$tmpdir/ivi.list
ivires=$tmpdir/ivi.result
tizenlist=$tmpdir/tizen.list
tizenres=$tmpdir/tizen.result

## sources files
ivimanifest=$tmpdir/ivi.manifest.xml
commonmanifest=$tmpdir/common.manifest.xml
gerritfile=$tmpdir/gerrit.json
gittreesfile=$tmpdir/git-trees

function getfiles () {
    echo "#### Generating required files"
    rm -rf $tmpdir
    mkdir $tmpdir
    mkdir $outdir
    echo -e "\nCloning scm/meta/git project"
    git clone review.tizen.org:scm/meta/git $tmpdir/git

    if [[ -d $tmpdir/git ]]; then
	cp $tmpdir/git/git-trees $tmpdir
	cd $tmpdir/git/scripts && ./fetch-gerrit-project-info.sh
	cp $tmpdir/git/scripts/gerrit-project-info* $tmpdir/gerrit.json
    else
	echo "Project has not been correctly cloned!" && usage && exit 1
    fi
    
    echo -e "\nGetting manifest files"

    if wget -q --spider $iviurl/$1 > /dev/null; then
	wget -q -O $tmpdir/ivi.manifest.xml $iviurl/$1/builddata/manifest/$1_ia32.xml
    else
	echo "Wrong ivi snapshot !" && usage && exit 1
    fi

    if wget -q --spider $commonurl/$2 > /dev/null; then
	wget -q -O $tmpdir/common.manifest.xml $commonurl/$2/builddata/manifest/$2_x86_64.xml
    else
	echo "Wrong common snapshot !" && usage && exit 1
    fi

    echo "done !"

}

function genlists () {
    
    local tmp=/tmp/tizen.tmp
    
    xml sel -t -m //project -v @name -n $ivimanifest > $ivilist
    xml sel -t -m //project -v @name -n $commonmanifest > $commonlist
    cat $ivilist >> $tmp
    cat $commonlist >> $tmp
    cat $tmp | sort | uniq > $tizenlist
    
    rm -rf $tmp
}


function gendatas () {
    echo -e "\n#### Generating info on projects"
    while read line 
    do
	
	## get firstly the domain from the json file generated by the fetch-gerrit-project-info.sh script - the git-trees file doesn't contain all the 
	#  packages
	domain=$(grep -A4 $line $gerritfile | sed -n '5p' | awk -F":" '{print $3}' | sed -e 's/\\u0026/\&/g' -e 's/"//g' -e 's/,//g' | awk -F";" '{print $1}' | sed 's/^ *//g' )

	# get the domain and the subdomain from the git-trees file.
	domain2=$(grep -A1 -sr $line $gittreesfile | sed -n '2p' | sed -r 's/^.{3}//' | sed 's/ \/ /\//g' | awk -F"/" '{print $1}')
	subdomain=$(grep -A1 -sr $line $gittreesfile | sed -n '2p' | sed -r 's/^.{3}//' | sed 's/ \/ /\//g' | awk -F"/" '{print $2}')
	[[ -z $subdomain ]] && subdomain="-"
	[[ -z $domain ]] && domain=$domain2
	[[ -z $domain ]] && domain="-"
	
	if grep -sr $line $ivilist >/dev/null && grep -sr $line $commonlist >/dev/null; then
	    vertical="common" 
	    echo "$line : $domain : $subdomain : $vertical" >> $commonres
	    echo "$line : $domain : $subdomain : $vertical" >> $ivires
	    echo "$domain" >> $commontmp1
	    echo "$domain" >> $ivitmp1
	    echo "$subdomain" >> $commontmp2
	    echo "$subdomain" >> $ivitmp2
	elif grep -sr $line $commonlist >/dev/null; then
	    vertical="pure common"
	    echo "$line : $domain : $subdomain : $vertical" >> $commonres
	    echo "$domain" >> $commontmp1
	    echo "$subdomain" >> $commontmp2
	elif grep -sr $line $ivilist >/dev/null; then
	    vertical="pure ivi"
	    echo "$line : $domain : $subdomain : $vertical" >> $ivires
	    echo "$domain" >> $ivitmp1
	    echo "$subdomain" >> $ivitmp2
	else
	    echo "cannot classify project" && exit 1
	fi

	echo "$line : $domain : $subdomain : $vertical" >> $tizenres
	echo "$domain" >> $tizentmp1
	echo "$subdomain" >> $tizentmp2
    done < $tizenlist

    echo -e "\n########## Stats on Domains ##########" >> $commonres
    sort $commontmp1 | uniq -c | sed 's/^ *//g' >> $commonres
    echo -e "\n########## Stats on Domains ##########" >> $ivires
    sort $ivitmp1 | uniq -c | sed 's/^ *//g' >> $ivires
    echo -e "\n########## Stats on Domains ##########" >> $tizenres
    sort $tizentmp1 | uniq -c | sed 's/^ *//g' >> $tizenres
    echo -e "\n\n########## Stats on Sub domains ##########" >> $commonres
    sort $commontmp2 | uniq -c | sed 's/^ *//g' >> $commonres
    echo -e "\n\n########## Stats on Sub domains ##########" >> $ivires
    sort $ivitmp2 | uniq -c | sed 's/^ *//g' >> $ivires
    echo -e "\n\n########## Stats on Sub domains ##########" >> $tizenres
    sort $tizentmp2 | uniq -c | sed 's/^ *//g' >> $tizenres

    rm -rf $commontmp1
    rm -rf $commontmp2
    rm -rf $ivitmp1
    rm -rf $ivitmp2
    rm -rf $tizentmp1
    rm -rf $tizentmp2
    
    echo -e "\ndone !"
    cp $tmpdir/*.list $tmpdir/*.result $outdir && echo -e "\nResult are available in the $outdir directory"
    rm -rf $tmpdir/*.list $tmpdir/*.result
}

function usage () {
    echo "$(basename $0) : 
	Usage : <ivi snapshot id> <common snapshot id>
	Example ./$(basename $0) tizen_20140527.2 tizen_20140527.12
	
	Note  : snapshot ids are id of 'snapshots' images"
	exit 1
}


if [[ -z $1 ]] || [[ -z $2 ]]; then
    usage
else
    getfiles $1 $2
    genlists
    gendatas
fi
